package gandalf.servlet;

import gandalf.controller.CartJpaController;
import gandalf.controller.RezervacijaJpaController;
import gandalf.controller.StrankaJpaController;
import gandalf.controller.VstopnicaJpaController;
import gandalf.model.Postavka;
import gandalf.model.Rezervacija;
import gandalf.model.Stranka;
import gandalf.model.Vstopnica;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Cart extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            // iz seje dobimo id_stranke, ki je prijavljena in direktno v
            // obstoječo košarico dodamo novo postavko in jo shranimo v bazo
            // če košarica še ne obstaja ustvarimo novo, dodamo postavko in shranimo

            Long idVstopnice = null;
            VstopnicaJpaController vstopnicaDao = new VstopnicaJpaController();
            CartJpaController cartDao = new CartJpaController();
            StrankaJpaController strankaDao = new StrankaJpaController();
            RezervacijaJpaController rezervacijaDao= new RezervacijaJpaController();

            Long strankaId = getIdStrankeIzSeje(request);
            Stranka stranka = null;
            if (strankaId != null) {
                stranka = strankaDao.findStranka(strankaId);
            }

            Vstopnica v = null;
            Postavka p = null;
            List<Postavka> postavke = new ArrayList<Postavka>();

            // gremo cez vse postavke in dodamo v kosarico
            int steviloVstopnic = Integer.parseInt(request.getParameter("st_vstopnic"));
            int kolicina = 0;
            for (int i = 0; i < steviloVstopnic; i++) {
                p = new Postavka();
                kolicina = Integer.parseInt(request.getParameter("kolicina" + i));
                if (kolicina > 0) {
                    idVstopnice = new Long(request.getParameter("id_vstopnica" + i));
                    v = vstopnicaDao.findVstopnica(idVstopnice);
                    p.setVstopnica(v);
                    p.setKolicina(kolicina);

                    postavke.add(p);
                }
            }

            
            if (request.getParameter("submit").equals("Dodaj")) {

               Long idKosarice = getIdKosariceIzSeje(request);


                if (idKosarice != null) {
                    gandalf.model.Cart c = cartDao.findCart(idKosarice);
                    c.setStranka(stranka);
                    // dodati moram vse nove postavke
                    c.updatePostavke(postavke);

                    cartDao.edit(c);
                } else if (idKosarice == null) {
                    gandalf.model.Cart c = new gandalf.model.Cart();
                    c.setStranka(stranka);
                    c.setPostavka(postavke);
                    // shranim kosarico
                    Long id_kosarice = cartDao.create(c);

                    // id_kosarice shranim v sejo za nadaljno uporabo
                    request.getSession().setAttribute("kosarica_id", id_kosarice);
                }

            } else {
                Long idRezervacije = getIdRezervacijeIzSeje(request);
                

                if (idRezervacije != null) {
                    gandalf.model.Rezervacija r = rezervacijaDao.findRezervacija(idRezervacije);
                    r.setStranka(stranka);
                    r.setDatum(new Date());
                    // dodati moram vse nove postavke
                    r.updatePostavke(postavke);
                    System.out.println("postavke updejtane! \n");

                    rezervacijaDao.edit(r);
                } else if (idRezervacije == null) {
                    Rezervacija r = new Rezervacija();
                    r.setStranka(stranka);
                    r.setPostavka(postavke);
                    r.setDatum(new Date());
                    
                    // shranim kosarico
                    Long id_rezervacije = rezervacijaDao.create(r);

                    // id_kosarice shranim v sejo za nadaljno uporabo
                    request.getSession().setAttribute("rezervacija_id", id_rezervacije);
                }


                
            }
            response.sendRedirect("index.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Long getIdStrankeIzSeje(HttpServletRequest request) {
        Long id = null;
        Object o = request.getSession().getAttribute("stranka_id");
        if (o != null && o instanceof Long) {
            id = (Long) o;
        }

        return id;
    }

    private Long getIdKosariceIzSeje(HttpServletRequest request) {
        Long id = null;
        Object o = request.getSession().getAttribute("kosarica_id");
        if (o != null && o instanceof Long) {
            id = (Long) o;
        }

        return id;
    }
    private Long getIdRezervacijeIzSeje(HttpServletRequest request) {
        Long id = null;
        Object o = request.getSession().getAttribute("rezervacija_id");
        if (o != null && o instanceof Long) {
            id = (Long) o;
        }

        return id;
    }
}
